package com.ruoyi.wq.service.impl;

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wq.domain.WqTuanHome;
import com.ruoyi.wq.domain.vo.WqTuanHomeVO;
import com.ruoyi.wq.mapper.WqTuanHomeMapper;
import com.ruoyi.wq.service.IWqTuanHomeService;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

/**
 * 拼团详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-28
 */
@Service
public class WqTuanHomeServiceImpl extends ServiceImpl<WqTuanHomeMapper, WqTuanHome> implements IWqTuanHomeService {

    @Autowired
    private WqTuanHomeMapper wqTuanHomeMapper;
    
    /**
     * 查询拼团详情
     * 
     * @param id 拼团详情主键
     * @return 拼团详情
     */
    @Override
    public WqTuanHome selectWqTuanHomeById(String id)
    {
        return wqTuanHomeMapper.selectWqTuanHomeById(id);
    }

    /**
     * 查询拼团详情列表
     * 
     * @param wqTuanHome 拼团详情
     * @return 拼团详情
     */
    @Override
    public List<WqTuanHome> selectWqTuanHomeList(WqTuanHome wqTuanHome)
    {
        return wqTuanHomeMapper.selectWqTuanHomeList(wqTuanHome);
    }

    /**
     * 新增拼团详情
     * 
     * @param wqTuanHome 拼团详情
     * @return 结果
     */
    @Override
    public int insertWqTuanHome(WqTuanHome wqTuanHome)
    {
        wqTuanHome.setCreateTime(DateUtils.getNowDate());
        return wqTuanHomeMapper.insertWqTuanHome(wqTuanHome);
    }

    /**
     * 修改拼团详情
     * 
     * @param wqTuanHome 拼团详情
     * @return 结果
     */
    @Override
    public int updateWqTuanHome(WqTuanHome wqTuanHome)
    {
        wqTuanHome.setUpdateTime(DateUtils.getNowDate());
        return wqTuanHomeMapper.updateWqTuanHome(wqTuanHome);
    }

    /**
     * 批量删除拼团详情
     * 
     * @param ids 需要删除的拼团详情主键
     * @return 结果
     */
    @Override
    public int deleteWqTuanHomeByIds(String[] ids)
    {
        return wqTuanHomeMapper.deleteWqTuanHomeByIds(ids);
    }

    /**
     * 删除拼团详情信息
     * 
     * @param id 拼团详情主键
     * @return 结果
     */
    @Override
    public int deleteWqTuanHomeById(String id)
    {
        return wqTuanHomeMapper.deleteWqTuanHomeById(id);
    }

	@Override
	public WqTuanHome getTuanHome(Long userId, String tuanId) {
		QueryWrapper<WqTuanHome> queryWrapper = new QueryWrapper<>();
		WqTuanHome entity = new WqTuanHome();
		entity.setUserId(userId);
		entity.setTuanId(tuanId);
		entity.setStatus("0");
		queryWrapper.setEntity(entity);
		WqTuanHome tuanHome = baseMapper.selectOne(queryWrapper);
		return tuanHome;
	}

	@Override
	public Integer getTuanHomeNumByTuanId(String tuanId) {
		QueryWrapper<WqTuanHome> queryWrapper = new QueryWrapper<>();
		WqTuanHome entity = new WqTuanHome();
		entity.setTuanId(tuanId);
		queryWrapper.setEntity(entity);
		queryWrapper.in("status", "1","2");
		return baseMapper.selectCount(queryWrapper);
	}

	@Override
	public TableDataInfo getPageList(WqTuanHome wqTuanHome) {
		IPage<WqTuanHomeVO> page = baseMapper.selectPageList(Condition.getPage(wqTuanHome), wqTuanHome);
		for(WqTuanHomeVO vo : page.getRecords()){
			long time = DateUtil.between(new Date(), vo.getTuanTime(), DateUnit.SECOND);
			vo.setTime(time);
			vo.setChaNum(vo.getUserNum()-vo.getPayNum());
		}
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}
	
}
